import http from '../../utils/http';
import * as types from '../mutations-type'
import {ORDER_EVALUATE} from "../mutations-type";

const state = {
    cartNum: 0,
    destineGoodsSubmit: {},
    orderActive: 0
};

const actions = {

    //获取分类列表
    [types.GET_CATEGORY_LIST]({}) {
        return new Promise((resolve) => {
            http.post('/category.lists').then((res) => {
                return resolve(res.data);
            });
        });
    },

    //商品搜索(分类、关键字)
    [types.SEARCH_GOODS]({}, content) {
        return new Promise((resolve) => {
            http.post('/prd.search', content).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //获取商品详情
    [types.GET_GOODS_DETAIL]({}, goodsId) {
        return new Promise((resolve) => {
            http.post('/prd.info', goodsId).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //获取商品评价列表
    [types.GET_GOODS_EVALUATE]({}, content) {
        return new Promise((resolve) => {
            http.post('/prd.cmt_list', content).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //加入购物车
    [types.CART_ADD]({dispatch}, content) {
        return new Promise((resolve) => {
            http.post('/user/cart.add', content).then((res) => {
                if (res.data.code !== 0) return
                dispatch(types.CART_NUM)
                return resolve(res.data);
            });
        });
    },
    //购物车列表
    [types.CART_LIST]({}) {
        return new Promise((resolve) => {
            http.post('/user/cart.list').then((res) => {
                return resolve(res.data);
            });
        });
    },
    //查询购物车数量
    [types.CART_NUM]({commit}, content) {
        return new Promise((resolve) => {
            http.post('/user/cart.query', content).then((res) => {
                if (res.data.code !== 0) return
                commit(types.SET_CART_NUM, res.data.total)
                return resolve(res.data);
            });
        });
    },
    //修改购物车数量
    [types.CART_EDIT]({dispatch, commit}, content) {
        return new Promise((resolve) => {
            http.post('/user/cart.set_num', content).then((res) => {
                if (res.data.code !== 0) return
                dispatch(types.CART_NUM)
                return resolve(res.data);
            });
        });
    },
    //删除购物车
    [types.CART_DEL]({dispatch}, ids) {
        return new Promise((resolve) => {
            http.post('/user/cart.del', ids).then((res) => {
                if (res.data.code !== 0) return
                dispatch(types.CART_NUM)
                return resolve(res.data);
            });
        });
    },
    //预下单
    [types.CART_PRE]({}, content) {
        return new Promise((resolve) => {
            http.post('/user/order.pre', content).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //下单
    [types.CART_CREATE_ORDER]({}, form) {
        return new Promise((resolve) => {
            http.post('/user/order.create', form).then((res) => {
                return resolve(res.data);
            });
        });
    },

    //订单列表
    [types.ORDER_LIST]({}, form) {
        return new Promise((resolve) => {
            http.post('/user/order.lists', form).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //确认收货
    [types.ORDER_RECEIPT]({}, form) {
        return new Promise((resolve) => {
            http.post('/user/order.confirm', form).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //申请退货
    [types.ORDER_REJECT]({}, form) {
        return new Promise((resolve) => {
            http.post('/user/order.reject', form).then((res) => {
                return resolve(res.data);
            });
        });
    },

    //评价商品
    [types.ORDER_EVALUATE]({}, form) {
        return new Promise((resolve) => {
            http.post('/user/order.cmt', form).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //促销商品列表
    [types.PRO_GOODS_LIST]({}, form) {
        return new Promise((resolve) => {
            http.post('/prd.discount', form).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //礼包商品列表
    [types.GIFT_GOODS_LIST]({}, form) {
        return new Promise((resolve) => {
            http.post('/gift.list', form).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //礼包商品详情
    [types.GIFT_GOODS_DETAIL]({}, form) {
        return new Promise((resolve) => {
            http.post('/gift.info', form).then((res) => {
                return resolve(res.data);
            });
        });
    }

};


const mutations = {
    //购物车数量
    [types.SET_CART_NUM](statINVEST_REGISTERe, num) {
        state.cartNum = num
    },

    //购物车预下单
    [types.SET_CART_SUBMIT](state, info) {
        state.destineGoodsSubmit = info
    },
    //储存进入订单列表状态
    [types.SET_ORDER_STATUS](state, orderActive) {
        state.orderActive = orderActive
    }

};


export default {
    state,
    actions,
    mutations,
    getters: {
        cartNum: state => state.cartNum,
        destineGoodsSubmit: state => state.destineGoodsSubmit,
        orderActive: state => state.orderActive,

    }
}
